
import EventDispatcher from "../event/EventDispatcher";
import GlobalEvent from "../event/GlobalEvent";
import LocalValue from "../storage/LocalValue";
import ResLoader from "../res/ResLoader";
import MusicManager from "../../engine/MusicManager";
import SoundManager from "../../engine/SoundManager";
import { logInfo } from "../tools/Define";
export default class AudioManager extends EventDispatcher {

    private soundVolume: LocalValue;
    private musicVolume: LocalValue;
    private musicFlag: LocalValue;
    private soundFlag: LocalValue;
    //已经开启的音乐名称 说明有音乐正在播放
    private openMusicName: string
    protected musicMgr: MusicManager;
    protected soundMgr: SoundManager
    protected loader: ResLoader;
    constructor(prjectName: string, loader: ResLoader) {
        super();
        this.loader = loader;
        this.musicMgr = new MusicManager(this.loader);
        this.soundMgr = new SoundManager(this.loader);
        GlobalEvent.instance().on(GlobalEvent.EVENT_HIDE, this.eventHide, this)
        GlobalEvent.instance().on(GlobalEvent.EVENT_SHOW, this.eventShow, this)
        this.init(prjectName);
    }

    init(prjectName) {
        this.soundVolume = new LocalValue(prjectName + 'soundVolume', 1);
        this.musicFlag = new LocalValue(prjectName + 'musicFlag', 1);
        this.musicVolume = new LocalValue(prjectName + 'musicVolume', 1);
        this.soundFlag = new LocalValue(prjectName + 'soundFlag', 1)
    }

    /**
     * 切到后台
     */
    eventHide() {
        logInfo(' eventHide this.getMusicFlag()', this.getMusicFlag())
        if (this.getMusicFlag()) {
            this.pause();
        }

    }
    // isShowing: boolean = false;
    /**
     * 切回前台
     */
    eventShow() {


        if (this.getMusicFlag()) {

            this.resume();

        } else {
       
        }

    }

    //暂停
    pause() {


    }
    //恢复
    resume() {


    }


    /**
     * 受标记影响，但是会记录想要播放的音乐名称。
     * 播放音乐，但是音乐时关闭状态，所以保留音乐名称。
     * @param name 
     * @param isLoop 
     */

    /**
     * 通过设置主动播放音乐
     * @param name 
     * @param isLoop 
     */
    playMusic(name: string, isLoop: boolean = true) {
        if (this.musicMgr.isPlaying()) {
            return;
        }
        this.musicMgr.play(name, isLoop)


    }

    /**
     * 通过设置主动关闭音乐
     */
    stopMusic() {
        if (!this.musicMgr.isPlaying()) {
            return;
        }
        this.openMusicName = null;
        this.musicMgr.stop()
    }


    /**
     * 播放音效
     * @param name 
     * @param canMulply 
     */
    playSound(name: any, canMulply: boolean = true) {
        this.soundMgr.play(name, canMulply)
    }


    //获取音效音量
    getSoundVolume() {
        if (!this.soundVolume) {
            return 1;
        }
        return this.soundVolume.getValue();
    }

    //获取音乐音量
    getMusicVolume() {
        if (!this.musicVolume) {
            return 1;
        }
        return this.musicVolume.getValue();
    }

    //获取音乐开关标识
    getMusicFlag() {
        if (!this.musicFlag) {
            return false;
        }
        let num = this.musicFlag.getValue();
        return num == 1 ? true : false;
    }





    //设置音乐音量
    setMusicVolume(num: number) {
        if (!this.musicVolume) {
            return;
        }
        this.musicVolume.setValue(num);
        this.musicMgr.setVolume(num)


    }

    //设置音效音量
    setSoundVolume(num: number) {
        if (!this.soundVolume) {
            return;
        }
        this.soundVolume.setValue(num);
        this.soundMgr.setVolume(num)
    }

    //设置音效开关标识
    setSoundFlag(f: boolean) {
        this.soundFlag.setValue(f ? 1 : 0)
    }

    getSoundFlag() {
        return this.soundFlag.getValue() == 1 ? true : false
    }

    //设置音乐开关标识
    setMusicFlag(flag: boolean) {
        if (!this.musicFlag) {
            return;
        }
        let num = flag ? 1 : 0;
        this.musicFlag.setValue(num);
    }

    //停止某个音效
    stopSound(soundName: string) {
        this.soundMgr.stop(soundName)
    }

    //停止所有音效
    stopAllSound() {
        this.soundMgr.stopAll();
    }
}
